package tests;

import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.Irr;
import org.bouncycastle.asn1.x509.Time;
import org.openqa.selenium.By;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.mysql.jdbc.Driver;

import framework.BaseFormExtended;
import framework.BaseFormExtended.Portal;
import framework.BaseTestExtended;
import framework.BrowserExtended;
import framework.ExcelManager;
import framework.advs.BaseAdvertisment;
import framework.portals.IrrTutBy;
import framework.portals.NeagentBy;
import framework.portals.PortalManager;

/**
 * Тест на проверку работоспособности некоторых ссылок
 */
public class CollectRealtyData extends BaseTestExtended {
	
	private static int counter;
	private Portal currentPortalId;
	private static ArrayList<BaseAdvertisment> irrBaseList, irrNewList, neagentBaseList, neagentNewList = new ArrayList<BaseAdvertisment>();
	private static ExcelManager excel = new ExcelManager();
	private static IrrTutBy irrTutBy;
	private static NeagentBy neagentBy;
	private static BaseFormExtended portal;
	private static boolean isChanged;

	/**
	 * Метод запуска тестов
	 */
	@Override
	public void runTest() {

		super.runTest();
		LogStep(); // 
		initialize();
		
		info("Собираем первоначальный список с портала: "+currentPortalId.toString());
		neagentBaseList = getListOfAdvs(currentPortalId);
		info("Выгружаем первоначальный список объявлений");
		neagentBy = getNeagInstance();
        neagentBy.collectAdvsData(neagentBaseList, currentPortalId);
		excel.exportDataToExcel(neagentBaseList, currentPortalId);
		
		/*currentPortalId=Portal.IRR_TUT_BY;
		info("Собираем первоначальный список с портала: "+currentPortalId.toString());
		irrBaseList = getListOfAdvs(currentPortalId);
		irrTutBy = getIrrInstance();
		info("Выгружаем первоначальный список объявлений");
		
		irrTutBy.collectAdvsData(irrBaseList, currentPortalId);
		excel.exportDataToExcel(irrBaseList, currentPortalId);*/

		/*long beginTime = System.currentTimeMillis();
		long cycleTimeout = 1800000;
		long iterationTimeout = 60000;
		while ((beginTime + cycleTimeout)>System.currentTimeMillis()){
			info("ожидаем таймаут итерации");
			browser.sleep(iterationTimeout);
			currentPortalId = Portal.NEAGENT_BY;
			neagentNewList = getListOfAdvs(currentPortalId);
			isChanged = neagentBy.isNewAdvAppeared(neagentBaseList, neagentNewList);
			updateIfChanged(currentPortalId);

			currentPortalId = Portal.IRR_TUT_BY;
			irrNewList = getListOfAdvs(currentPortalId);
			isChanged = irrTutBy.isNewAdvAppeared(irrBaseList, irrNewList);

			updateIfChanged(currentPortalId);
			counter++;
			info("текущая итерация: "+counter);
		}*/
	}

	private void updateIfChanged(Portal p) {
		if (isChanged){
		
		switch (p) {
		case IRR_TUT_BY:
			irrBaseList = irrNewList;	
			irrTutBy.collectAdvsData(BaseFormExtended.getResultList(), p);
			break;
		case NEAGENT_BY:
			neagentBaseList = neagentNewList;		
			neagentBy.collectAdvsData(BaseFormExtended.getResultList(), p);
			break;
		default:
			break;
		           }
		excel.exportDataToExcel(BaseFormExtended.getResultList(), currentPortalId);
		info("На портале "+currentPortalId.toString()+ " появилось " + BaseFormExtended.getResultList().size() + " новых объявлений");
		}
	}

	private ArrayList<BaseAdvertisment>  getListOfAdvs(Portal portal) {
		ArrayList<BaseAdvertisment>  list = new ArrayList<BaseAdvertisment>();
			browserExt.navigateToWindow(portal);
			switch (portal) {
			case IRR_TUT_BY:
				irrTutBy = getIrrInstance();
				list = irrTutBy.getListOfAdvs();				
				break;
			case NEAGENT_BY:
				neagentBy = getNeagInstance();
				list = neagentBy.getListOfAdvs();
				break;
			default:
				break;
			}
			return list;
		}

	private void initialize() {
		CollectRealtyData.counter = 0;
		currentPortalId = Portal.NEAGENT_BY;
		//currentPortalId = Portal.IRR_TUT_BY;
	}
	
	private IrrTutBy getIrrInstance(){
		if (irrTutBy == null){
			irrTutBy = new IrrTutBy();
		}
		return irrTutBy;
	}
	private NeagentBy getNeagInstance(){
		if (neagentBy == null){
			neagentBy = new NeagentBy();
		}
		return neagentBy;
	}
}
